/**
 ****************************************************************
 * @file felica.h
 *
 * @brief 
 *
 * @author 
 *
 * 
 ****************************************************************
 */
#ifndef FELICA_H
#define FELICA_H

/** \name Lengths
*/
/*@{*/
/** UID length in bytes. */
#define PHPAL_FELICA_IDM_LENGTH     8U
/** PMm length in bytes. */
#define PHPAL_FELICA_PMM_LENGTH     8U
/** Block length in bytes. */
#define PHPAL_FELICA_BLOCK_LENGTH   4U
/** Number of bytes per page. */
#define PHPAL_FELICA_PAGE_LENGTH    16U
/** Length of ATQC. */
#define PHPAL_FELICA_ATQC_LENGTH    18U
/** Length of Request Data (RD) in ATQC. */
#define PHPAL_FELICA_RD_LENGTH      2U
/** Maximum length of a transmission package. */
#define PHPAL_FELICA_TXLENGTH_MAX   254U
/*@}*/
#define FELICA_REQUEST_RESPONSE_RSP_LEN	10U

#define FELICA_WRITE_WITHOUT_ENCRYPTION_RSP_LEN	12U

//#define FELICA_REQUEST_SERVICE_RSP_LEN	21U
/**
* \name Definitions of the time slots for the ReqC command
*/
/*@{*/
#define PHPAL_FELICA_NUMSLOTS_1     0x00    /**< Value for \c bNumSlots = 1. */
#define PHPAL_FELICA_NUMSLOTS_2     0x01    /**< Value for \c bNumSlots = 2. */
#define PHPAL_FELICA_NUMSLOTS_4     0x03    /**< Value for \c bNumSlots = 4. */
#define PHPAL_FELICA_NUMSLOTS_8     0x07    /**< Value for \c bNumSlots = 8. */
#define PHPAL_FELICA_NUMSLOTS_16    0x0F    /**< Value for \c bNumSlots = 16. */
/*@}*/

#define PHAC_DISCLOOP_TYPEF_DEFAULT_TIME_SLOT                 0x00U              /**< Default value for time slot parameter for detecting Felica tags. */

#define	FELICA_FDT_302us	2048

/** \name Timing constants
*/
/*@{*/
#define PHPAL_FELICA_SW_TIME_T_US           302U
#define PHPAL_FELICA_SW_EXT_TIME_US         120U    /**< Time extension in microseconds for all commands.  */
#define PHPAL_FELICA_SW_RESP_TIME_A_US      2417U//(2672U + PHPAL_FELICA_SW_EXT_TIME_US)
#define PHPAL_FELICA_SW_RESP_TIME_B_US      1208U
/*@}*/

/** \name Felica Command Codes
*/
/*@{*/
#define PHPAL_FELICA_SW_CMD_REQC            0x00U   /**< RequestC. */
#define PHPAL_FELICA_SW_CMD_REQ_RESP        0x04U   /**< Request Response. */
#define PHPAL_FELICA_SW_CMD_REQ_SERVICE     0x02U   /**< Request Service. */
#define PHPAL_FELICA_SW_CMD_READ            0x06U   /**< Read. */
#define PHPAL_FELICA_SW_CMD_WRITE           0x08U   /**< Write. */
#define PHPAL_FELICA_SW_CMD_REQ_SYS_CODE	0x0CU
#define PHPAL_FELICA_SW_CMD_AUTH1           0x10U   /**< Authentication1. */
#define PHPAL_FELICA_SW_CMD_AUTH2           0x12U   /**< Authentication2. */
#define PHPAL_FELICA_SW_CMD_READ_SECF       0x14U   /**< Read from secure file. */
#define PHPAL_FELICA_SW_CMD_WRITE_SECF      0x16U   /**< Write to secure file. */
#define PHPAL_FELICA_SW_RSP_REQC            0x01U   /**< Response to RequestC. */
#define FELICA_REQUEST_RESPONSE_RES			0x05U
#define FELICA_REQUEST_SERVIVE_RES			0x03U
#define FELICA_READ_WITHOUT_ENCRYPTION_RES	0x07U
#define FELICA_WRITE_WITHOUT_ENCRYPTION_RES	0x09U
#define FELICA_REQUEST_SYS_CODE_RES			0x0DU

#define PHHAL_HW_FELICA_ADD_DELAY_US      240U        /**< Additional digital timeout delay for Felica. */

#define PHHAL_HW_TIMER_FREQ               13.56f      /**< RC internal timer frequency. */
#define PHHAL_HW_TIMER_MAX_VALUE_US       39586813    /**< RC maximum timer value in microseconds. */
#define PHHAL_HW_TIMER_SHIFT              4.8f        /**< Shift of the internal RC timer in ETUs. */
#define PHHAL_HW_ETU_106                  9.44f       /**< Duration of one ETU at 106 kBit/s in [us]. */

#define PHHAL_HW_BIT_TAUTO            0x80U   /**< Sets the Timer start/stop conditions to Auto mode. */
#define PHHAL_HW_BIT_TAUTORESTART     0x10U   /**< Restarts the timer automatically after counting down to 0. */
#define PHHAL_HW_MASK_TGATED          0x60U   /**< Bitmask for TGated bits. */
#define PHHAL_HW_MASK_TPRESCALER_HI   0x0FU   /**< Bitmask for TPrescalerHi bits. */

#define PHHAL_HW_BIT_RXMULTIPLE   0x04U   /**< Activates reception mode for multiple responses. */

#define PHHAL_HW_BIT_424KBPS      0x20U   /**< Activates speed of 424kbps. */
#define PHHAL_HW_BIT_212KBPS      0x10U   /**< Activates speed of 212kbps. */

void pcd_set_felica_tmo(u8 n, u8 Position);

void pcd_set_reqc_tmo(u16 wTimeout, u16 wDataRate);

int pcd_request_c(u8 *pSysCode, u8 bReqCode, u8 bSlot, u8 bRate, u8 *pResponse);

int pcd_req_resp(u8 *pIdmPMm, u8 *pMode);

int pcd_req_service(u8 *pIdmPMm, 
						 u8  bTxNumServices, 
                         u8 *pTxServiceList, 
                         u8 *pRxNumServices, 
                         u8 *pRxServiceList
                         );


int pcd_read_without_encryption(u8 *pIdmPMm, 
								         u8  bNumServices, 
								         u8 *pServiceList, 
								         u8  bTxNumBlocks, 
								         u8 *pBlockList,
								         u8  bBlockListLength,
								         u8 *pRxNumBlocks,
								         u8 *pBlockData,
								         u8 *pStatusFlag
								         );

int pcd_write_without_encryption(u8 *pIdmPMm, 
									   	  u8  bNumServices, 
									      u8 *pServiceList, 
									      u8  bNumBlock, 
									      u8 *pBlockList,
									      u8  bBlockListLength,
									      u8 *pBlockData,
									      u8 *pStatusFlag
									   );

int pcd_req_syscode(u8 *pIdmPMm,  u8 *pSysCodeNum, u8 *pSysCodeList);


#endif
